﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Collector
{
    public partial class ScanSn : Form
    {
        // 标记用户已经输入， 关闭窗体时无需通知
        private bool mInputFinished = false;

        public ScanSn()
        {
            InitializeComponent();

            label_inputInfo.Text = "Input Length = " + InputSn.Text.Length.ToString();
            if(FileConfig.WriteFixedFrequency())
            {
                this.Fixed_Frequency.Enabled = true;
            }
            else
            {
                this.Fixed_Frequency.Enabled = false;
            }

            if (FileConfig.WritePCBSN())
            {
                this.InputSn.Enabled = true;
            }
            else
            {
                this.InputSn.Enabled = false;
            }

            textBox1.Text = FileConfig.getmmHgList("EmployeeNumber");
        }

        public event SerialNumberInputedEventHandler SerialNumberInputed;
        private void OnSerialNumberInputed(SerialNumberInputedEventArgs e)
        {
            if(SerialNumberInputed != null)
            {
                SerialNumberInputed.Invoke(this, e);
            }
            //SerialNumberInputed?.Invoke(this, e);
        }

        private void WT_Number_KeyUp(object sender, KeyEventArgs e)
        {
            this.label_inputInfo.Text = "Input Length = " + this.Fixed_Frequency.Text.Length.ToString();
            if (e.KeyData != Keys.Return)
            {
                return;
            }
            if (FileConfig.WritePCBSN())
            {
                InputSn.Focus();
                return;
            }

            string moduleNumber = this.Fixed_Frequency.Text.Trim();
            string serialNumber = this.InputSn.Text.Trim();
            mInputFinished = true;
            OnSerialNumberInputed(new SerialNumberInputedEventArgs(moduleNumber, serialNumber));
            MessageControl.Text = String.Format("Input OK .... {0}, SN =  {1}", this.Fixed_Frequency.Text, this.InputSn.Text);

            this.Fixed_Frequency.Text = "";
            this.InputSn.Text = "";
            this.Fixed_Frequency.Focus();
            this.Hide();
        }

        private void InputSn_KeyUp(object sender, KeyEventArgs e)
        {
            this.label_inputInfo.Text = "Input Length = " + this.InputSn.Text.Length.ToString();
            if (e.KeyData != Keys.Return)
            {
                return;
            }

            string moduleNumber = this.Fixed_Frequency.Text.Trim();
            string serialNumber = this.InputSn.Text.Trim();
            if (FileConfig.WriteFixedFrequency())
            {
                if (moduleNumber.Length < 1 || moduleNumber.Length > 20)
                {
                    MessageControl.Text = String.Format("WT Number ranges from WT01~WT08... Please Check");
                    this.Fixed_Frequency.Text = "";
                    this.InputSn.Text = "";
                    this.Fixed_Frequency.Focus();
                    return;
                }
            }

            if (InputSn.Text.Trim().Length != 16)
            {
                MessageControl.Text = String.Format("Sn:{0}, length != 16, please Check ..", InputSn.Text);
                this.InputSn.Text = "";
                this.InputSn.Focus();
                return;
            }

            // 标记用户已经输入， 关闭窗体时无需通知
            mInputFinished = true;
            OnSerialNumberInputed(new SerialNumberInputedEventArgs(moduleNumber, serialNumber));
            MessageControl.Text = String.Format("Input OK .... {0}, SN =  {1}", Fixed_Frequency.Text, InputSn.Text);

            this.Fixed_Frequency.Text = "";
            this.InputSn.Text = "";
            if (FileConfig.WriteFixedFrequency())
                this.Fixed_Frequency.Focus();
            else
                this.InputSn.Focus();
            this.Hide();

        }

        private void ScanSn_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Hide();
            CloseReason r = e.CloseReason;
            e.Cancel = true;  //不关闭Form。只隐藏                        

            // 如果用户没有输入SN或者频率， 将会上报通知上层，用于解除阻塞
            if (!mInputFinished)
            {
                OnSerialNumberInputed(new SerialNumberInputedEventArgs(null, null, true));
                mInputFinished = true;
            }
        }

        private void ScanSn_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                this.Close();
            }
        }
    }

    public delegate void SerialNumberInputedEventHandler(object sender, SerialNumberInputedEventArgs e);
    public class SerialNumberInputedEventArgs : EventArgs
    {
        // 设备固定频率
        private string moduleIndex;
        // 设备SN
        private string serialNumber;
        // 退出原因
        private bool isClosed;

        public SerialNumberInputedEventArgs(string mudule, string sn, bool closed = false)
        {
            this.moduleIndex = mudule;
            this.serialNumber = sn;
            this.isClosed = closed;
        }
        public string ModuleIndex
        {
            get
            {
                return moduleIndex;
            }
        }
        public string SerialNumber
        {
            get
            {
                return serialNumber;
            }
        }

        public bool Closed
        {
            get
            {
                return isClosed;
            }
        }
    }
}
